package com.woniuxy.controller;

import com.woniuxy.entity.*;
import com.woniuxy.service.*;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * (OrdermanageDetails)表控制层
 *
 * @author makejava
 * @since 2021-05-24 11:19:24
 */
@RestController
@RequestMapping("ordermanageDetails")
public class OrdermanageDetailsController {
    /**
     * 服务对象
     */
    @Resource
    private OrdermanageDetailsService ordermanageDetailsService;

    @Resource
    private OrdermanageService ordermanageService;

    @Resource
    private IProjectsService iProjectsService;

    @Resource
    private StaffService staffService;

    @Resource
    private SortService sortService;


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public OrdermanageDetails selectOne(Integer id) {

        return this.ordermanageDetailsService.queryById(id);
    }

    /**
     *  
     * Date: 20:08:2021/5/24  <br/>
     * Description:增加子订单
     */
    @PostMapping("insertOrderDetail")
    @ApiOperation("增加子订单")
    public ResponseEntity<OrdermanageDetails> insertOrderDetail(@RequestBody OrdermanageDetails ordermanageDetails){
        System.out.println("新增子订单" + ordermanageDetails);
        //获取子订单的父订单id
        Integer orderdtFid = ordermanageDetails.getOrderdtFid();
        //获取父订单
        Ordermanage ordermanage = this.ordermanageService.queryById(orderdtFid);

//      //子订单中插入父订单的编号
//      ordermanageDetails.setOrderdtCode(ordermanage.getOrderNumber());

        //获取项目具体信息
        IProjects iProjects = this.iProjectsService.queryById(ordermanageDetails.getOrderdtServiceid());
        //子订单中插入项目名称
        ordermanageDetails.setOrderdtServicename(iProjects.getIprjName());
        //子订单插入项目价钱
        ordermanageDetails.setOrderdtServiceprice(iProjects.getIprjSellPrice());
        //子订单中插入项目图片
        ordermanageDetails.setOrderdtImage(iProjects.getIprjCoverImg());

        //获取父订单总金额
        Double orderTotal = ordermanage.getOrderTotal();

        //获取子订单的金额
        Double orderdtServiceprice = ordermanageDetails.getOrderdtServiceprice();

        //更新父订单总金额
        orderTotal = orderTotal + orderdtServiceprice;

        //更新父订单对象
        ordermanage.setOrderTotal(orderTotal);

        //往数据库更新父订单
        this.ordermanageService.update(ordermanage);

        //获取技师id
        Integer staffid = ordermanageDetails.getOrderdtStaffid();

        //前端传入技师id情况（点技师）
        if (staffid != null){
            //获取技师具体数据
            Staff staff = this.staffService.queryById(staffid);
            //获取技师名字
            ordermanageDetails.setOrderdtStaffname(staff.getStaffName());
            //获取技师状态
            Integer staffStatus=this.staffService.selectstatusbystaffId(staffid).getStaffStatus();
            //修改技师状态为技师原来状态+4
            staffStatus=staffStatus%4+4;
            this.staffService.updatestatusbystaffId(staffid,staffStatus);
        }
        //前端不传入技师id情况（轮巡）
        else{
            Sort sortbegin = this.sortService.sortbegin();
            Staff staff = this.staffService.queryById(sortbegin.getSortStaffid());
            ordermanageDetails.setOrderdtStaffid(staff.getStaffId());
            ordermanageDetails.setOrderdtStaffname(staff.getStaffName());
        }

        //往数据库添加子订单
        OrdermanageDetails insert = this.ordermanageDetailsService.insert(ordermanageDetails);

        if (insert != null){
            return new ResponseEntity(1,"插入子订单成功" , insert);
        }
        return new ResponseEntity(0,"插入子订单失败");
    }


    /**
     *  
     * Date: 20:44:2021/5/24  <br/>
     * Description:修改上钟时间
     */
    @PutMapping("updateUptime")
    @ApiOperation("修改上钟时间")
    public ResponseEntity updateUptime(@RequestBody OrdermanageDetails ordermanageDetails){
        //获取技师id
        Integer staffid = ordermanageDetails.getOrderdtStaffid();
        //判断技师的状态
        Integer staffStatus=this.staffService.selectstatusbystaffId(staffid).getStaffStatus();
        if(staffStatus>=4){
            this.sortService.sortorder(staffid);
        }else{
            this.sortService.sortupdatestatus(staffid);
        }
        OrdermanageDetails update = this.ordermanageDetailsService.update(ordermanageDetails);
        if (update != null){
            return new ResponseEntity(1,"修改上钟时间成功");
        }
        return new ResponseEntity(0,"修改上钟时间失败");
    }


    /**
     *  
     * Date: 20:49:2021/5/24  <br/>
     * Description:修改下钟时间
     */
    @PutMapping("updateDowntime")
    @ApiOperation("修改下钟时间")
    public ResponseEntity updateDowntime(@RequestBody OrdermanageDetails ordermanageDetails){
        System.out.println("修改下钟时间"+ordermanageDetails);

        //定义一个变量 用来判断子订单中是否存在未完成的订单
        boolean boo = true;

        //获取技师id
        Integer staffid = ordermanageDetails.getOrderdtStaffid();
        //通过技师id获取技师状态
        Integer staffStatus=this.staffService.selectstatusbystaffId(staffid).getStaffStatus();
        //状态为2 被点
        if(staffStatus==2){
            this.sortService.sortorderfinish(staffid);
        }//状态为1 轮循工作中
        else{
            this.sortService.sortfinish(staffid);
        }

        //将子订单对象更新到数据库
        OrdermanageDetails update = this.ordermanageDetailsService.update(ordermanageDetails);


        //根据子订单id获取子订单对象
        OrdermanageDetails ordermanageDetails2 = this.ordermanageDetailsService.queryById(ordermanageDetails.getOrderdtId());
        //修改下钟时间判断所有子订单是否都已完成 若完成 更改大订单状态为1（未支付） 若存在子订单未完成 则更改大订单状态为3（项目未完成 ）
        //获取fid
        Integer orderdtFid = ordermanageDetails2.getOrderdtFid();
        //获取大订单对象
        Ordermanage ordermanage = this.ordermanageService.queryById(orderdtFid);

        //根据fid获取所有的子订单对象
        List<OrdermanageDetails> ordermanageDetails1 = this.ordermanageDetailsService.selectByFid(orderdtFid);
        for (OrdermanageDetails ordermanageDetail : ordermanageDetails1) {
            //如果存在子订单未完成的状态，则更改大订单状态为2（项目未完成）
            if (ordermanageDetail.getOrderdtEndtime() == null){
                ordermanage.setOrderStatus(2);
                boo = false;
            }
        }
        //如果boo为true 则说明所有子订单都已完成，则改变大订单状态为1（未支付）
        if (boo){
            ordermanage.setOrderStatus(0);
        }

        //将大订单对象更新到数据库
        this.ordermanageService.update(ordermanage);


        if (update != null){
            return new ResponseEntity(1,"修改下钟时间成功");
        }
        return new ResponseEntity(0,"修改下钟时间成功");
    }

    /**
     *  
     * Date: 11:22:2021/5/25  <br/>
     * Description:删除子订单
     */
    @DeleteMapping("deleteOrder")
    @ApiOperation("删除子订单")
    public ResponseEntity deleteOrder(@RequestBody OrdermanageDetails ordermanageDetails){
        //获取子订单ID
        Integer id = ordermanageDetails.getOrderdtId();
        //删除子订单
        boolean b = this.ordermanageDetailsService.deleteById(id);
        //获取子订单的技师id
        Integer staffId=ordermanageDetails.getOrderdtStaffid();
        //根据子订单的技师id查询技师状态(回退)
        Integer staffStatus=this.staffService.selectstatusbystaffId(staffId).getStaffStatus();
        if(staffStatus>=4){//状态小于4的状态不变
            staffStatus=staffStatus-4;//回退状态
            //修改为原状态
            this.staffService.updatestatusbystaffId(staffId,staffStatus);
        }
        if (b){
            return new ResponseEntity(1,"删除子订单成功");
        }
        return new ResponseEntity(0,"删除子订单失败");
    }



}
